/**
 * 报表设计公司(2011)
 */
package net.abbp.report.model.cell.style.background;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import net.abbp.swing.util.ImageUtils;
import com.reportgear.report.core.api.compare.ComparatorUtils;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class TextureBackground extends AbstractBackground {

	private static final Rectangle2D embededImageRect = new Rectangle2D.Double(0.0D, 0.0D, 58.0D, 42.0D);
	public static final TexturePaint NewsprintTexturePaint = new TexturePaint(
			createBufferedImage("background/Newsprint.gif"), embededImageRect);
	public static final TexturePaint RecycledPaperTexturePaint = new TexturePaint(
			createBufferedImage("background/RecycledPaper.gif"), embededImageRect);
	public static final TexturePaint ParchmentTexturePaint = new TexturePaint(
			createBufferedImage("background/Parchment.gif"), embededImageRect);
	public static final TexturePaint StationeryTexturePaint = new TexturePaint(
			createBufferedImage("background/Stationery.gif"), embededImageRect);
	public static final TexturePaint GreenMarbleTexturePaint = new TexturePaint(
			createBufferedImage("background/GreenMarble.gif"), embededImageRect);
	public static final TexturePaint WhiteMarbleTexturePaint = new TexturePaint(
			createBufferedImage("background/WhiteMarble.gif"), embededImageRect);
	public static final TexturePaint BrownMarbleTexturePaint = new TexturePaint(
			createBufferedImage("background/BrownMarble.gif"), embededImageRect);
	public static final TexturePaint GraniteTexturePaint = new TexturePaint(
			createBufferedImage("background/Granite.gif"), embededImageRect);
	public static final TexturePaint BlueTissuePaperTexturePaint = new TexturePaint(
			createBufferedImage("background/BlueTissuePaper.gif"), embededImageRect);
	public static final TexturePaint PinkTissuePaperTexturePaint = new TexturePaint(
			createBufferedImage("background/PinkTissuePaper.gif"), embededImageRect);
	public static final TexturePaint PurpleMeshTexturePaint = new TexturePaint(
			createBufferedImage("background/PurpleMesh.gif"), embededImageRect);
	public static final TexturePaint BouquetTexturePaint = new TexturePaint(
			createBufferedImage("background/Bouquet.gif"), embededImageRect);
	public static final TexturePaint PapyrusTexturePaint = new TexturePaint(
			createBufferedImage("background/Papyrus.gif"), embededImageRect);
	public static final TexturePaint CanvasTexturePaint = new TexturePaint(
			createBufferedImage("background/Canvas.gif"), embededImageRect);
	public static final TexturePaint DenimTexturePaint = new TexturePaint(
			createBufferedImage("background/Denim.gif"), embededImageRect);
	public static final TexturePaint WovenMatTexturePaint = new TexturePaint(
			createBufferedImage("background/WovenMat.gif"), embededImageRect);
	public static final TexturePaint WaterDropletsTexturePaint = new TexturePaint(
			createBufferedImage("background/WaterDroplets.gif"), embededImageRect);
	public static final TexturePaint PaperBagTexturePaint = new TexturePaint(
			createBufferedImage("background/PaperBag.gif"), embededImageRect);
	public static final TexturePaint FishFossilTexturePaint = new TexturePaint(
			createBufferedImage("background/FishFossil.gif"), embededImageRect);
	public static final TexturePaint SandTexturePaint = new TexturePaint(
			createBufferedImage("background/Sand.gif"), embededImageRect);
	public static final TexturePaint CorkTexturePaint = new TexturePaint(
			createBufferedImage("background/Cork.gif"), embededImageRect);
	public static final TexturePaint WalnutTexturePaint = new TexturePaint(
			createBufferedImage("background/Walnut.gif"), embededImageRect);
	public static final TexturePaint OakTexturePaint = new TexturePaint(
			createBufferedImage("background/Oak.gif"), embededImageRect);
	public static final TexturePaint MediumWoodTexturePaint = new TexturePaint(
			createBufferedImage("background/MediumWood.gif"), embededImageRect);
	private transient TexturePaint texturePaint;

	private static BufferedImage createBufferedImage(String paramString) {
		return GraphHelper.createBufferedImageFromImage(ImageUtils.readImage(paramString));
	}

	public TextureBackground() {
		this(null);
	}

	public TextureBackground(TexturePaint paramTexturePaint) {
		this.texturePaint = null;
		setTexturePaint(paramTexturePaint);
	}

	public void paint(Graphics paramGraphics, Shape paramShape) {
		Graphics2D localGraphics2D1 = (Graphics2D) paramGraphics;
		if (getTexturePaint() == null) {
			return;
		}
		Rectangle2D localRectangle2D1 = paramShape.getBounds2D();
		BufferedImage localBufferedImage1 = new BufferedImage((int) localRectangle2D1.getWidth(),
				(int) localRectangle2D1.getHeight(), 2);
		Graphics2D localGraphics2D2 = localBufferedImage1.createGraphics();
		BufferedImage localBufferedImage2 = getTexturePaint().getImage();
		Rectangle2D localRectangle2D2 = getTexturePaint().getAnchorRect();
		int i = (int) localRectangle2D2.getWidth();
		int j = (int) localRectangle2D2.getHeight();
		if ((i > 0) && (j > 0)) {
			int k = 0;
			while (k < localRectangle2D1.getHeight()) {
				int l = 0;
				while (l < localRectangle2D1.getWidth()) {
					localGraphics2D2.drawImage(localBufferedImage2, l, k, i, j, null);
					l += i;
				}
				k += j;
			}
		}
		localGraphics2D2.dispose();
		Shape localShape = localGraphics2D1.getClip();
		localGraphics2D1.clip(paramShape);
		localGraphics2D1.translate(localRectangle2D1.getX(), localRectangle2D1.getY());
		localGraphics2D1.drawImage(localBufferedImage1, 0, 0, null);
		localGraphics2D1.translate(-localRectangle2D1.getX(), -localRectangle2D1.getY());
		localGraphics2D1.setClip(localShape);
	}

	public TexturePaint getTexturePaint() {
		return this.texturePaint;
	}

	public void setTexturePaint(TexturePaint paramTexturePaint) {
		this.texturePaint = paramTexturePaint;
	}

	private void readObject(ObjectInputStream paramObjectInputStream) throws ClassNotFoundException, IOException {
		paramObjectInputStream.defaultReadObject();
		ShapeSerializable localShapeSerializable = (ShapeSerializable) paramObjectInputStream.readObject();
		Rectangle2D localRectangle2D = (Rectangle2D) localShapeSerializable.getShape();
		BufferedImage localBufferedImage = null;
		Object localObject = paramObjectInputStream.readObject();
		if (localObject != null) {
			localBufferedImage = GraphHelper.createBufferedImageFromImage(((ImageSerializable) localObject).getImage());
		}
		this.texturePaint = new TexturePaint(localBufferedImage, localRectangle2D);
	}

	private void writeObject(ObjectOutputStream paramObjectOutputStream) throws IOException {
		paramObjectOutputStream.defaultWriteObject();
		if (this.texturePaint == null) {
			return;
		}
		Rectangle2D localRectangle2D = this.texturePaint.getAnchorRect();
		ShapeSerializable localShapeSerializable = new ShapeSerializable(localRectangle2D);
		paramObjectOutputStream.writeObject(localShapeSerializable);
		BufferedImage localBufferedImage = this.texturePaint.getImage();
		if (localBufferedImage == null) {
			return;
		}
		ImageSerializable localImageSerializable = new ImageSerializable(localBufferedImage);
		paramObjectOutputStream.writeObject(localImageSerializable);
	}

	public boolean equals(Object paramObject) {
		if (!(paramObject instanceof TextureBackground)) {
			return false;
		}
		TextureBackground localTextureBackground = (TextureBackground) paramObject;
		return texturePaintEquals(getTexturePaint(), localTextureBackground.getTexturePaint());
	}

	private boolean texturePaintEquals(TexturePaint paramTexturePaint1, TexturePaint paramTexturePaint2) {
		if ((paramTexturePaint1 == null) || (paramTexturePaint2 == null)) {
			return ((paramTexturePaint1 == null) && (paramTexturePaint2 == null));
		}
		return ((ComparatorUtils.equals(paramTexturePaint1.getAnchorRect(), paramTexturePaint2.getAnchorRect())) && (ComparatorUtils
				.imageEquals(paramTexturePaint1.getImage(), paramTexturePaint2.getImage())));
	}
}
